//
//  LZCurrentUserInfo.m
//  laziz_Consumer
//
//  Created by ZhaoDongBo on 2016/11/17.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZCurrentUserInfo.h"
#import "GVUserDefaults+LZProperties.h"
#import "LZThirdPartyAccountModel.h"
#import "MJProperty.h"

@implementation LZCurrentUserInfo

- (id)mj_newValueFromOldValue:(id)oldValue property:(MJProperty *)property
{
    if ([oldValue isKindOfClass:[NSNull class]])
        return @"";
    
    return oldValue;
}

+ (NSDictionary *)mj_objectClassInArray
{
    return @{
             @"thirdParty" : @"LZThirdPartyAccountModel",
             };
}

#pragma mark - 实现NSCoding协议的方法

-(id)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super init]) {
        self.userId = [aDecoder decodeObjectForKey:@"userId"];
        self.userName = [aDecoder decodeObjectForKey:@"userName"];
        self.nickName = [aDecoder decodeObjectForKey:@"nickName"];
        self.cellphone = [aDecoder decodeObjectForKey:@"cellphone"];
        self.email = [aDecoder decodeObjectForKey:@"email"];
        self.userPhoto = [aDecoder decodeObjectForKey:@"userPhoto"];
        
        self.password = [aDecoder decodeBoolForKey:@"password"];
        self.messageUnRead = [aDecoder decodeBoolForKey:@"messageUnRead"];
        
        self.couponsNum = [aDecoder decodeIntegerForKey:@"couponsNum"];
        self.noReviewedNum = [aDecoder decodeIntegerForKey:@"noReviewedNum"];
        
        // 第三方绑定
        NSMutableArray *thirdPartyData = [aDecoder decodeObjectForKey:@"thirdParty"];
        if (self.thirdParty) {
            [self.thirdParty removeAllObjects];
        }
        else {
            self.thirdParty  = @[].mutableCopy;
        }
        for (int i = 0; i<thirdPartyData.count; i++) {
            LZThirdPartyAccountModel *model = (LZThirdPartyAccountModel *)[NSKeyedUnarchiver unarchiveObjectWithData: thirdPartyData[i]];
            [self.thirdParty addObject:model];
        }

    }
    return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.userId forKey:@"userId"];
    [aCoder encodeObject:self.userName forKey:@"userName"];
    [aCoder encodeObject:self.nickName forKey:@"nickName"];
    [aCoder encodeObject:self.cellphone forKey:@"cellphone"];
    [aCoder encodeObject:self.email forKey:@"email"];
    [aCoder encodeObject:self.userPhoto forKey:@"userPhoto"];
    
    [aCoder encodeBool:self.password forKey:@"password"];
    [aCoder encodeBool:self.messageUnRead forKey:@"messageUnRead"];
    
    [aCoder encodeInteger:self.couponsNum forKey:@"couponsNum"];
    [aCoder encodeInteger:self.noReviewedNum forKey:@"noReviewedNum"];
    
    
    // 第三方绑定
    NSMutableArray *thirdPartyData = @[].mutableCopy;
    for (int i = 0; i<self.thirdParty.count; i++) {
        NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:self.thirdParty[i]];
        [thirdPartyData addObject:myEncodedObject];
    }
    [aCoder encodeObject:thirdPartyData forKey:@"thirdParty"];
}

#pragma mark - 存用户信息

+(void)setUserInfoWithModel:(LZCurrentUserInfo*)model
{
    if (model == nil) {
        return;
    }
    // 序列化
    NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:model];
    [GVUserDefaults standardUserDefaults].userInfoModel = myEncodedObject;
}

#pragma mark - 获取存下的用户信息

+(LZCurrentUserInfo*)getUserInfoModel
{
    // 反序列化
    NSData *myEncodedObject = [GVUserDefaults standardUserDefaults].userInfoModel;
    if (myEncodedObject) {
        LZCurrentUserInfo *model = (LZCurrentUserInfo *)[NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject];
        return model;
    }
    
    return nil;
}

@end
